'turret shot.sdlbas [B+=MGA] 2016-10-22
'run missile shot to edge of screen  unless mouse click starts another shot

'2016-08-24 modified with Andy Amaya atan2 suggestion, his saucer target and collision tests
'2016-10-21 modified with turret for tanks
'2016-10-22 modify for drag power, draw turret to mouse position post 2016-10-23

const xmax = 800
const ymax = 600
const pi = acos(-1)
const gravity = .4
const airResistance = .975  'multiple
const turretLimit = 20
setdisplay(xmax, ymax, 32, 1)
setcaption("Caution: Angle Shot Test Site - live ammo!")
autoback(75)


level = 3 '<<<<<<<<<<<<<<<<<<<<<<<<< pick a level the lower the level the bigger and slower the traget

'global variables for subs and main loop
baseX = xmax/2 : baseY = ymax/2 'base center

'round variables to track speed and location of round/ missile / shot / projectile
rRate =0 : rActive = 0 : radAngle = 0 : rDX = 0 : rDY = 0 : rX = 0 : rY = 0 : rD = 5

'target variables
tX = 25 : tY = rnd(ymax/2) + ymax/4 : tDX =0 : tD = (7 - level) * 5 : tDir = 1

'turret exit point of round
ix = 0 : iy = 0

' ======================================= general procedures

function c2c(cx1, cy1, r1, cx2, cy2, r2)
    'Circle to Circle collision detection
    'cx1, cy1 are center coords of circle 1
    'r1 is radius of circle 1
    'cx2, cy2 are center coords of circle 2
    'r2 is radius of circle 2

    'Returns a 1 if collision detected else returns 0
    dx = cx2 - cx1
    dy = cy2 - cy1
    radii = r1 + r2
    if (dx * dx + dy * dy) <= (radii * radii) then
        c2c = 1
    else
        c2c = 0
    end if
end function

'==================================== procedures for main loop
sub newTarget()
	tX = rnd(xmax)  : tY = rnd(int( .8 * ymax) ) + int(  .1 * ymax)
	if rnd(3) - 1 then
		tDir = tDir * -1
	end if
end sub

sub drawSpace()
	ink(0) 'cls
	bar(0, 0, xmax, ymax)
end sub

sub drawBase()
	ink(0x0000ff) 'blue base
	fillcircle(baseX, baseY, 10)
end sub

sub drawtarget()
	'update position
	tX = tX + tDX * tDir
	if tX > xmax  or tX < 0 then
		newTarget()
	end if
	'draw target
	k = 0
	for j = tD to 5 step -5
		k +=1
		if k mod 2 then
			ink(0xff0000)
		else
			ink(0xffffff)
		end if
		fillcircle(tX, tY, j)
	next
end sub

sub drawRound()
	if rActive then 'update, determin if in bounds if so draw it, if not deactivate
		rDY += gravity
		rDY *= airResistance
		rDX *= airResistance
		rX += rDX
		rY += rDY
		if rX > 0 and rX < xmax and rY < ymax then ' still on screen
			'check for collision
			if c2c(rX, rY, rD, tX, tY, tD) then 'draw explosions Andy's style!
				Ink(0xFF0000)
				FillCircle(tX, tY, 60)
				Ink(0x808000)
                FillCircle(tX, tY, 40)
                Ink(0xffff00)
                FillCircle(tX, tY, 20)
				newTarget()
                rActive = 0     'deactivate round
			else
				'draw round at rX, rY
				ink(0xffff00)
				fillcircle(rX, rY, rD)
			end if
		else 'off screen , deactivate
			rActive = 0
		end if
	end if
end sub

sub turret(xBase, yBase, r1, rAng, lng, r2)
	xe = xBase + lng * cos(rAng)
	ye = yBase + lng * sin(rAng)
	dx = (xe - xBase) / lng
	dy = (ye - yBase) / lng
	dr = (r2 - r1) / lng
	for i = 0 to lng
		ix = xBase + i * dx
		iy = yBase + i * dy
		ir = r1 + i * dr
		fillcircle(ix, iy, ir)
	next
	ink(0)
	fillcircle(ix, iy, ir -2)
end sub

newTarget()
while key(27) = 0  '====================================== main loop
	drawSpace()
	'drawBase()
	drawTarget()
	drawRound()
	diffX = mouseX - baseX : diffY = mouseY - baseY
	rRate = (diffX ^ 2 + diffY ^ 2) ^ .5
	if rRate > turretLimit then : rRate = turretLimit : end if
	if rRate = 0 then : rRate = 1 : end if
	radAngle = atan2(diffY, diffX)
	ink(0xff0000)
	turret(baseX, baseY, 6, radAngle, rRate, 4)

	if mousebutton = 1 then 'fire another round
		rDX = rRate * cos(radAngle)
		rDY = rRate * sin(radAngle)
		rX = ix
		rY = iy
		rActive = 1
	end if

	'messages
	ink(0xffffff)
	text(3, 3, 18, "Level " + str(level) )

	waitVBL()
wend

